package com.boyi.shared.util;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.lang.reflect.Field;

/**
 * PojoToMultiValueMap.class
 *
 * @author tingwen
 * @date date
 */
public class PojoToMultiValueMap {
	public static MultiValueMap convert(Object pojo) {
		Class<?> clazz = pojo.getClass();
		MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
		while (clazz != Object.class) {
			Field[] fields = clazz.getDeclaredFields();
			for (Field field : fields) {
				try {
					if (!field.isAccessible()) {
						field.setAccessible(true);
					}
					if (field.get(pojo) == null) continue;
					multiValueMap.add(field.getName(), field.get(pojo));
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
			clazz = clazz.getSuperclass();
		}
		return multiValueMap;
	}
}
